
package com.musiklegal;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;



public class MainMenu extends Activity{
    /** Called when the activity is first created. */
	public static ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>();
	public static String username;
	public static final int Logout = Menu.FIRST + 1;
	public static final int TopUp = Menu.FIRST + 2;
	public static final int Subs = Menu.FIRST + 3;
	public static final int About = Menu.FIRST + 4;
	public static final int Help = Menu.FIRST + 5;
	public static final int DiscFB = Menu.FIRST + 6;
	
	Calendar ci = Calendar.getInstance();
	 String tid =""+ci.get(Calendar.YEAR) + "-" + 
   (ci.get(Calendar.MONTH) + 1) + "-" +
   ci.get(Calendar.DAY_OF_MONTH) + " " +
   ci.get(Calendar.HOUR) + ":" +
   ci.get(Calendar.MINUTE) +  ":" +
   ci.get(Calendar.SECOND);
	 
	public static MerchantEntity defaultMerchant = null;
	//static boolean defmerc = false;
	private ImageButton onlinecat,defaultcat,mydownload,myplaylist,setting,help;
	public static String endDate;
	boolean isConnected =false;
	HttpClient mlist;
	HttpGet get;
	HttpResponse mresp ;
	HttpEntity merclist;
	SAXParserFactory spf;
	SAXParser sp;
	XMLReader xr;
	MerchantParser mparse;
	CatalogueParser cp;
	static AlertDialog.Builder builder; 
	static String subsc;
	static int balance;
	
	static String defkey;
	static int defpos;
	static String defimg;
	static String defname;
	Handler h;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        h = new Handler();
        MusikLegal.mPreferences= getSharedPreferences("CurrentUser", MODE_PRIVATE);
        
        if(MusikLegal.mPreferences!=null &&MusikLegal.mPreferences.getString("Lang", "Indo").equalsIgnoreCase("English"))
		{	
			Settings.jhentisubs=Language.thentisubs;
			Settings.jmulaisubs=Language.tmulaisubs;
			Settings.jbangkrut=Language.tbangkrut;
			Settings.jteman=Language.tfriend;
			Settings.joption=Language.toption;

			Settings.mulaisubs=Language.emulaisubs;
			Settings.mulaisubs2=Language.emulaisubs2;
			Settings.hentisubs=Language.ask;
			Settings.bangkrut=Language.ebangkrut;
			Settings.gakonek=Language.egakonek;
			Settings.error=Language.eerror;
			Settings.thank=Language.ethanks;
			Settings.sd=Language.esd;
			Settings.expp=Language.eexpp;
			Settings.expd=Language.eexpd;
			Settings.del=Language.edel;
			Settings.deal=Language.edeal;
			Settings.playlist=Language.eplaylist;
			Settings.info=Language.einfo;
			Settings.nosos=Language.enosos;
			Settings.def=Language.edef;
			Settings.cari=Language.ecari;
			Settings.nodef=Language.enodef;
			Settings.help=Language.ehelp;
	    
			Settings.yes=Language.eya;
			Settings.no=Language.etidak;
			Settings.topup=Language.etop;
			Settings.logout=Language.elogout;
		}
        if(MusikLegal.mPreferences!=null)
        balance=Integer.parseInt(MusikLegal.mPreferences.getString("Balance","0"));
       // if(!MusikLegal.internet(getApplicationContext()))
        //	finish();
        //else
        new Thread(new Runnable(){

			public void run() {
				if(MusikLegal.internet(getApplicationContext()))
		        {
					
		        	
		        	
		        		try {
		        			MusikLegal.appContact(getApplicationContext());
		        			MusikLegal.checkSubscription(getApplicationContext());
		        			h.post(new Runnable(){
		        				
								public void run() {
									// TODO Auto-generated method stub
									setStatus(MainMenu.this);
								}});
		        			
		        			if(MusikLegal.log!=null&&MusikLegal.log.listSong.size()>0)
							MusikLegal.log.send();
						} catch (IllegalStateException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (SAXException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (ParserConfigurationException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();		
		        	}
		        }
			}}).start();
        
        
        setContentView(R.layout.menuview);

        builder = new AlertDialog.Builder(this);
        onlinecat = (ImageButton) findViewById(R.id.ocat);
        defaultcat = (ImageButton) findViewById(R.id.defaultcat);
        mydownload = (ImageButton) findViewById(R.id.mydownload);
        myplaylist = (ImageButton) findViewById(R.id.myplaylist);
        setting = (ImageButton) findViewById(R.id.settings);
        help  = (ImageButton) findViewById(R.id.help);
        
        MusikLegal.mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);
        if(!MusikLegal.on)
        {
        	
        	subsc = Settings.jmulaisubs;
        }
        else
        {
        	
        	subsc = Settings.jhentisubs;
        }
        //setStatus(this);
        defaultcat.setOnClickListener(new OnClickListener(){

			public void onClick(View v) 
			{
				if(defaultMerchant == null)
					Toast.makeText(getApplicationContext(), Settings.nodef, Toast.LENGTH_SHORT).show();
				else if(!MusikLegal.internet(getApplicationContext()))
				{
					Toast.makeText(getApplicationContext(), Settings.gakonek, Toast.LENGTH_SHORT).show();
					//finish();
				}
				else
				{
					Browseby.pos=defpos;
					Browseby.apikey=MusikLegal.mPreferences.getString("defkey", "1");
					Browseby.imurl=MusikLegal.mPreferences.getString("defimg","a");
					Browseby.name=MusikLegal.mPreferences.getString("defname","a");
					startActivity(new Intent(getApplicationContext(),Browseby.class));
				}
				
			}});
        
        onlinecat.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				if(MusikLegal.internet(getApplicationContext()))
				startActivity(new Intent(getApplicationContext(),OnlineC.class));
				else
					{
						Toast.makeText(getApplicationContext(), Settings.gakonek, Toast.LENGTH_SHORT).show();
						//finish();
					}
			}});
        mydownload.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				startActivity(new Intent(getApplicationContext(),MyD.class));
				
			}});
        myplaylist.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				
					startActivity(new Intent(getApplicationContext(),MyPlayList.class));
        		
				
				
			}});
        setting.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				startActivity(new Intent(getApplicationContext(),Settings.class));
				
			}});
        help.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				if(!MusikLegal.mPreferences.getString("FBId", "0").equals("0"))
					startActivity(new Intent(getApplicationContext(),Friend.class));
					//Toast.makeText(getApplicationContext(), Settings.nosos, Toast.LENGTH_SHORT).show();
				else if (!MusikLegal.mPreferences.getString("TWId", "0").equals("0"))
					startActivity(new Intent(getApplicationContext(),Friend.class));
					//Toast.makeText(getApplicationContext(), Settings.nosos, Toast.LENGTH_SHORT).show();
				else
					Toast.makeText(getApplicationContext(), Settings.nosos, Toast.LENGTH_SHORT).show();
			}});
              
    }
	public boolean onCreateOptionsMenu(Menu menu){

		menu.add(0,Logout,0,Settings.logout);

		menu.add(0,TopUp,0,Settings.topup);
		
		menu.add(0,Subs,0,subsc);
		
		menu.add(0,Help,0,Settings.help);
		
		menu.add(0,About,0,"Inbox");

		return true;

		}
	public boolean onOptionsItemSelected (MenuItem item){

		switch (item.getItemId()){

		case Logout:
		logout();

		return true;

		case TopUp :
			
			AlertDialog.Builder alert = new AlertDialog.Builder(this);

			   alert.setTitle("Topup by Hard Voucher");
			   alert.setMessage("Masukkan 16 digit kode voucher");
		 
			   final EditText input = new EditText(this);
			   alert.setView(input);
			   alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				   public void onClick(DialogInterface dialog, int whichButton) {
				     
					   	String vouch = input.getText().toString();
					   	DefaultHttpClient client = new DefaultHttpClient();
				        HttpPost httppost = new HttpPost("http://topup.musiklegal.com/webservice.php");
				        List< BasicNameValuePair > nvps = new ArrayList< BasicNameValuePair >();
				        nvps.add(new BasicNameValuePair("username", MusikLegal.mPreferences.getString("UserName", "default")));
				        nvps.add(new BasicNameValuePair("cid", tid));
				        nvps.add(new BasicNameValuePair("voucher", vouch));
				 
				        try {   
				      	  	UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
				            
				            httppost.setEntity(p_entity);
				            
				            HttpResponse response = client.execute(httppost);
				            HttpEntity responseEntity = response.getEntity();
				            

				            SAXParserFactory spf = SAXParserFactory.newInstance();
				            SAXParser sp = spf.newSAXParser();
				            XMLReader xr = sp.getXMLReader();
				            
				            LoginHandler myLoginHandler = new LoginHandler();
				            xr.setContentHandler(myLoginHandler);
				            xr.parse(retrieveInputStream(responseEntity));
				            ParsedLoginDataSet parsedLoginDataSet = myLoginHandler.getParsedLoginData();
				           
				            
				            if (parsedLoginDataSet.getExtractedString().equals("SUCCESS")) 
				            {
				            	Toast.makeText(getApplicationContext(), "Saldo Musiklegal anda berhasil di topup", Toast.LENGTH_SHORT).show();
				            	
				            } else  
				            {
				            	Toast.makeText(getApplicationContext(), "Invalid voucher code", Toast.LENGTH_SHORT).show();
				            }
				      } catch (Exception e)
				      {
				          
				      }
				     }
				   });

				   alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				     public void onClick(DialogInterface dialog, int whichButton) {
				      
				     }
				   });
				   alert.setIcon(R.drawable.oc2);
				   alert.show();  
		return true;
		
		case Subs:
			if(MusikLegal.internet(getApplicationContext())){
			if(subsc.equals("Stop Subscription")||subsc.equals("Berhenti berlangganan"))
			{	
				
				AlertDialog.Builder dialog2 = new AlertDialog.Builder(this);
            	dialog2.setTitle(subsc);
            	dialog2.setMessage(Settings.hentisubs);
            	//dialog2.setCancelable(false);
            	dialog2.setPositiveButton(Settings.yes, new DialogInterface.OnClickListener() {
            	public void onClick(DialogInterface dialog, int id) 
            	{	           
            	   String username = MusikLegal.mPreferences.getString("UserName", "default");
 				   String uid = MusikLegal.getUID(getApplicationContext());
 			       String pass = MusikLegal.mPreferences.getString("PassWord", "default");
 			       String subid = MusikLegal.mPreferences.getString("SubId", "default") ;
 			    	
 				   DefaultHttpClient client = new DefaultHttpClient();
 			       HttpPost httppost = new HttpPost("http://secure.musiklegal.com/subscription/stop");
 			       List< BasicNameValuePair > nvps = new ArrayList< BasicNameValuePair >();
 			       nvps.add(new BasicNameValuePair("USERNAME", username));
 			       nvps.add(new BasicNameValuePair("UID", uid));
 			       nvps.add(new BasicNameValuePair("PASSWORD", pass));
 			       nvps.add(new BasicNameValuePair("SUBSCRIPTION_ID", subid));

 			       try {   
 			    	    UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
	    	            
	    	            httppost.setEntity(p_entity);
	    	            
	    	            HttpResponse response = client.execute(httppost);
	    	            HttpEntity responseEntity = response.getEntity();
	    	            

	    	            SAXParserFactory spf = SAXParserFactory.newInstance();
	    	            SAXParser sp = spf.newSAXParser();
	    	            XMLReader xr = sp.getXMLReader();
	    	            
	    	            LoginHandler myLoginHandler = new LoginHandler();
	    	            xr.setContentHandler(myLoginHandler);
	    	            xr.parse(retrieveInputStream(responseEntity));
	    	            ParsedLoginDataSet parsedLoginDataSet = myLoginHandler.getParsedLoginData();
	    	           
	    	            Editor e = MusikLegal.mPreferences.edit();
 			           if (parsedLoginDataSet.getExtractedString().equals("SUCCESS")) 
 			           {
 			        	  
 			              e.remove("START").commit();
 			              e.remove("SubId").commit();
 			              e.remove("SubDate").commit();
 			              e.commit();
 			              MusikLegal.end_subscription=null;
 			              MusikLegal.on =false;
 			             if(MyD.currentSong!=null&&MyD.isInited)
 			    		 {
 			    		 MyD.currentSong.isplaying=false;
 			    		 MyD.player.reset();
 			    		 MyD.isInited=false;
 			    		 MyD.paused=false;
 			    		 MyD.currentSong=null;
 			    		 }
 			              
 			              subsc=Settings.jmulaisubs;
 			              setStatus(MainMenu.this);
 			        	  Toast.makeText(getApplicationContext(), "Stop Subscription Succes", Toast.LENGTH_SHORT).show();
 			           } 
 			           else 
 			           {
 			        	  Toast.makeText(getApplicationContext(), Settings.error, Toast.LENGTH_SHORT).show();
 			                  
 			           }
 			           
 			     } catch (Exception e)
 			     {
 			    	Toast.makeText(getApplicationContext(), Settings.error, Toast.LENGTH_SHORT).show();
 					
 			     }
            	}
            	});
            	dialog2.setNegativeButton(Settings.no, new DialogInterface.OnClickListener() { 
            		public void onClick(DialogInterface dialog, int which) {
            			dialog.dismiss();
            	} });
            	dialog2.create();
            	dialog2.setIcon(R.drawable.oc2);
                dialog2.show(); 
		
			}
			else if ((MusikLegal.mPreferences.getString("Balance", "0").equals("0"))){
				AlertDialog.Builder dialog = new AlertDialog.Builder(this);
	        	dialog.setTitle(Settings.jbangkrut);
	        	dialog.setMessage(Settings.bangkrut);
	        	dialog.setCancelable(false);
	        	dialog.setPositiveButton("Input Voucher", new DialogInterface.OnClickListener() {
	        	public void onClick(DialogInterface dialog, int id)
	        	{
	        		startActivity(new Intent(getApplicationContext(), Voucher.class));
	        	}
	        	});
	        	dialog.setNegativeButton("SMS Subscribe", new DialogInterface.OnClickListener() { 
	        		public void onClick(DialogInterface dialog, int which) {
	        			startActivity(new Intent(getApplicationContext(), TopUp.class));
	        	} });
	        	dialog.create();
	        	dialog.setIcon(R.drawable.oc2);
	            dialog.show();
			}
			else if(subsc.equals("Start Subscription")||subsc.equals("Mulai berlangganan"))
	    	{
				AlertDialog.Builder dialog = new AlertDialog.Builder(this);
	        	dialog.setTitle(Settings.jmulaisubs);
	        	dialog.setMessage(Settings.mulaisubs+MusikLegal.mPreferences.getString("Balance", "0")+Settings.mulaisubs2);
	        	dialog.setCancelable(false);
	        	dialog.setPositiveButton(Settings.yes, new DialogInterface.OnClickListener() {
	        	public void onClick(DialogInterface dialog, int id)
	        	{
	        		String username = MusikLegal.mPreferences.getString("UserName", "default");
	    			String uid = MusikLegal.getUID(getApplicationContext());
	    		    String pass = MusikLegal.mPreferences.getString("PassWord", "default");
	    				
	    			DefaultHttpClient client = new DefaultHttpClient();
	    	        HttpPost httppost = new HttpPost("http://secure.musiklegal.com/subscription/start_weekly");
	    	        List< BasicNameValuePair > nvps = new ArrayList< BasicNameValuePair >();
	    	        nvps.add(new BasicNameValuePair("USERNAME", username));
	    	        nvps.add(new BasicNameValuePair("UID", uid));
	    	        nvps.add(new BasicNameValuePair("PASSWORD", pass));
	    	 
	    	        try {   
	    	      	  	UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
	    	            
	    	            httppost.setEntity(p_entity);
	    	            
	    	            HttpResponse response = client.execute(httppost);
	    	            HttpEntity responseEntity = response.getEntity();
	    	            

	    	            SAXParserFactory spf = SAXParserFactory.newInstance();
	    	            SAXParser sp = spf.newSAXParser();
	    	            XMLReader xr = sp.getXMLReader();
	    	            
	    	            LoginHandler myLoginHandler = new LoginHandler();
	    	            xr.setContentHandler(myLoginHandler);
	    	            xr.parse(retrieveInputStream(responseEntity));
	    	            ParsedLoginDataSet parsedLoginDataSet = myLoginHandler.getParsedLoginData();
	    	           
	    	           

	    	            
	    	            if (parsedLoginDataSet.getExtractedString().equals("SUCCESS")) 
	    	            {
	    	            	 SharedPreferences.Editor editor=MusikLegal.mPreferences.edit();
	   	                  
	    	            	 
	 			              endDate = parsedLoginDataSet.getDate();
	    	            	if(parsedLoginDataSet.getSiD()!=null)
	                       {
	    	            		editor.remove("START").commit();
		 			              editor.remove("SubId").commit();
		 			              editor.remove("SubDate").commit();
		    	            	editor.putString("SubDate", parsedLoginDataSet.getDate()).commit();
		    	            	editor.putString("START", parsedLoginDataSet.getStart()).commit();
		                       	editor.putString("SubId", parsedLoginDataSet.getSiD()).commit();
	                       }
	    	            	
	    	            	MusikLegal.on = true;
	    	            	subsc=Settings.jhentisubs;
	    	            	setStatus(MainMenu.this);
	    	            	Toast.makeText(getApplicationContext(), Settings.thank, Toast.LENGTH_SHORT).show();
	    	        	    
	    	                 
	    	            } else  
	    	            {
	    	            	Toast.makeText(getApplicationContext(), Settings.error, Toast.LENGTH_SHORT).show();
	    	            }
	    	      } catch (Exception e)
	    	      {
	    	          
	    	      }
	        	}
	        	});
	        	dialog.setNegativeButton(Settings.no, new DialogInterface.OnClickListener() { 
	        		public void onClick(DialogInterface dialog, int which) {
	        			dialog.dismiss();
	        	} });
	        	dialog.create();
	        	dialog.setIcon(R.drawable.oc2);
	            dialog.show();
			
	    	}
			else
	        {
	        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
			alertDialog.setTitle("Error");
			alertDialog.setMessage(Settings.gakonek);
			alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
				public void onClick(DialogInterface dialog, int which) {
				//finish();
				}
			});
			alertDialog.setIcon(R.drawable.oc2);
			alertDialog.show();
	        }
			
			}
		return true;
		
		case Help:
			if(MusikLegal.internet(getApplicationContext()))
			{
				LoginWeb.p = MainMenu.this;
				LoginWeb.urlTo="http://playerandroidhelp.musiklegal.com/";
				startActivity(new Intent(getApplicationContext(),LoginWeb.class));
				
			}
			else
				{Toast.makeText(getApplicationContext(), Settings.gakonek, Toast.LENGTH_SHORT).show();
				//finish();
				}
		return true;
		
		case About:
			
			if(MusikLegal.internet(getApplicationContext()))
			{
				startActivity(new Intent(getApplicationContext(),Inbox.class));
				
			}
			else
				{Toast.makeText(getApplicationContext(), Settings.gakonek, Toast.LENGTH_SHORT).show();
				//finish();
				}
			return true;
		}
		return false;
	}


	public void  logout()
    {
		
		if(MyD.currentSong!=null&&MyD.isInited)
		{
		 MyD.currentSong.isplaying=false;
		 MyD.player.reset();
		 MyD.isInited=false;
		 MyD.paused=false;
		 MyD.currentSong=null;
		 }
         SharedPreferences.Editor editor=MusikLegal.mPreferences.edit();
         editor.clear();
         editor.commit();
         Intent mainIntent = new Intent(this, MusikLegal.class);
         //mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
         MusikLegal.on=false;
         if(MusikLegal.db!=null)
         MusikLegal.db.closeDB();
         LoginWeb.fromSettings=false;
         Inbox.ibd.clear();
         System.gc();
         //finish();
         startActivity(mainIntent);
         ImageLoader.clearCache();
         finish();
    }
	 
	private InputSource retrieveInputStream(HttpEntity httpEntity) {
        InputSource insrc = null;
        try {
              insrc = new InputSource(httpEntity.getContent());
        } catch (Exception e) {
        }
        return insrc;
  }
	public void onResume()
	{
		super.onResume();
		
		new Thread(new Runnable(){

			public void run() {
				if(!MusikLegal.internet(getApplicationContext()))
				{
					try {
						MusikLegal.appContact(getApplicationContext());
					} catch (ClientProtocolException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ParserConfigurationException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SAXException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					h.post(new Runnable(){

						public void run() {
							Toast.makeText(getApplicationContext(), Settings.gakonek, Toast.LENGTH_SHORT).show();
							// TODO Auto-generated method stub
							
						}});
					
					//finish();
				}
				h.post(new Runnable(){

					public void run() {
						MainMenu.setStatus(MainMenu.this);
						
					}});
					if(checkStatus()<1)
						{
						Log.d("status", checkStatus()+"");
							MusikLegal.on=false;
							subsc = Settings.jmulaisubs;
						}
					else
					{
						MusikLegal.on=true;
						subsc = Settings.jhentisubs;
					}
					
					if(MusikLegal.internet(getApplicationContext())&&MusikLegal.log!=null&&MusikLegal.log.listSong.size()>0)
		        	{
		        		try {
							MusikLegal.log.send();
						} catch (IllegalStateException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (SAXException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (ParserConfigurationException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
		        	}
				
				
			}}).start();
		
//		String end = MusikLegal.mPreferences.getString("SubDate", null);
//		Date curr = null;
//		if(MusikLegal.current_date!=null)
//		 curr = PlaylistSender.parseToUTC(MusikLegal.current_date);
//		
//		Date endd = null;
//		if(end!=null)
//		 endd = PlaylistSender.parseToUTC(end);
//		
//		if((curr!=null&&endd!=null)&&PlaylistSender.durationDay(curr, endd)>0)
//			MusikLegal.on =;
//		else
//			MusikLegal.on = false;	
		
		OnlineC.catl.clear();
		Inbox.ibd.clear();
		TrackInfo.fromFriend=false;
		if(!MusikLegal.on)
        	subsc = Settings.jmulaisubs;
        else
        	subsc = Settings.jhentisubs;
		
	}
	
	static long checkStatus()
	{
		String now = MusikLegal.current_date;
		String end = MusikLegal.mPreferences.getString("SubDate", null);
		Date n =null;
		//Log.d("tanggal sekarang",now);
		if(now!=null)
			n = PlaylistSender.parseToUTC(now);
		else 
			n=Calendar.getInstance().getTime();
		
		Date e =null;
		if(end!=null)
			e= PlaylistSender.parseToUTC(end);
		
		if(e!=null)
			return PlaylistSender.durationDay(n, e);
		
		else
			
		return 0;
	}
	
	public boolean onPrepareOptionsMenu(Menu menu)
	{
		menu.clear();
		menu.add(0,Logout,0,Settings.logout);
		menu.add(0,TopUp,0,Settings.topup);
		menu.add(0,Subs,0,subsc);
		menu.add(0,Help,0,Settings.help);
		menu.add(0,About,0,"Inbox");
		return true;
	}
	
	public void onDestroy()
	{
		super.onDestroy();
		MusikLegal.db.closeDB();
	}
	
	static void setStatus(Activity a)
	{
		 if(MusikLegal.on)
			{
			 	long duration=-1;
	        	String end = MusikLegal.mPreferences.getString("SubDate", null);
	        	Date cur=null;
	        	if(MusikLegal.current_date==null)
	        		cur = Calendar.getInstance().getTime();
	        	if(end!=null)
	        	{
	        		if(cur==null)
		        	{
	        			duration = PlaylistSender.durationDay(PlaylistSender.parseToUTC(MusikLegal.current_date),PlaylistSender.parseToUTC(end));
		        		//Log.i("tes",PlaylistSender.parseToUTC(MusikLegal.current_date).toString());
		        	}
	        		else
	        		{
	        			duration = PlaylistSender.durationDay(cur,PlaylistSender.parseToUTC(end));
	        			if(duration>-14)
	        				duration=1;
	        			//Log.i("tes", cur.toString());
	        		}
	        	}
	        	if(duration>0)
	        		a.setTitle("SOCIAL MUSIC NETWORKS \t"+duration+((duration==1)?" day ":" days ")+"freeplay");
	        	else
	        		a.setTitle("SOCIAL MUSIC NETWORKS \t 3x Free Play");
	        }
			else
				a.setTitle("SOCIAL MUSIC NETWORKS \t 3x Free Play");
		
	}
}